﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using GurLoader.CommandLineOptions;
using NUnit.Framework;

namespace GurLoaderWPF.Test.CommandLineOptions
{
    [TestFixture]
    public class CommandLineParsingExceptionTests
    {
        [Test]
        public void CreateInstanceTest()
        {
            CommandLineParsingException exception = new CommandLineParsingException();

            Assert.IsNotNull(exception);            
        }

        [Test]
        public void CreateInstanceStringTest()
        {
            string message = "Test Error Message";

            CommandLineParsingException exception = new CommandLineParsingException(message);

            Assert.IsNotNull(exception);
            Assert.AreEqual(message, exception.Message);
        }

        [Test]
        public void CreateInstanceStringInnerExceptionTest()
        {
            Exception innerEx = new Exception("Inner Exception");
            string message = "Test Error Message";

            CommandLineParsingException exception = new CommandLineParsingException(message, innerEx);

            Assert.IsNotNull(exception);

            Assert.AreEqual(message, exception.Message);
            Assert.AreEqual(innerEx, exception.InnerException);
        }

        [Test]
        public void CreateInstanceFromSerializationTest()
        {
            string message = "Test Error Message";
            
            CommandLineParsingException exception = new CommandLineParsingException(message);

            CommandLineParsingException afterDeserializationException;

            using (Stream s = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(s, exception);
                s.Position = 0; // Reset stream position
                afterDeserializationException = (CommandLineParsingException)formatter.Deserialize(s);
            }

            Assert.IsNotNull(afterDeserializationException);
            Assert.AreEqual(message, afterDeserializationException.Message);
        }
    }
}
